﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IP_PROJECT
{
    public partial class Galery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            FileUpload fileUpload = (FileUpload)AdminView.FindControl("FileUploadControl");
            TextBox imageName = (TextBox)AdminView.FindControl("ImageName");
            TextBox imageDescription = (TextBox)AdminView.FindControl("ImageDescription");
            if (fileUpload.HasFile)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                Image img = new Image();
                img.Content = fileUpload.FileBytes;
                img.Name = imageName.Text;
                img.Description = imageDescription.Text;
                db.Images.InsertOnSubmit(img);
                db.SubmitChanges();
            }
            Response.Redirect(Request.Url.ToString());
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            String id = ((Button)sender).CommandArgument;
            DataClassesDataContext db = new DataClassesDataContext();
            var result = from u in db.Images
                         where u.Id == Int32.Parse(id)
                         select u;
            Image image = result.First();
            db.Images.DeleteOnSubmit(image);
            db.SubmitChanges();
            Response.Redirect(Request.Url.ToString());
        }
    }
}